package com.yiquan.user.controller;

import com.yiquan.user.pojo.User;
import com.yiquan.user.service.LoginService;
import com.yiquan.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import java.util.HashMap;

/**
 * @author Tyrone
 * @date 2021/12/24 20:47
 */
@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserService userService;

    /**
     * 普通账号密码登录
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/loginBySimpleWay")
    public Result loginBySimpleWay(@RequestParam String username, @RequestParam String password) {
        if (username == null || password == null) {
            return new Result(false, StatusCode.PARAM_ERROR, "参数异常");
        }
        User user = loginService.loginBySimpleWay(username, password);
        if (user == null)
            return new Result(false, StatusCode.ERROR, "登录失败");
        String token = jwtUtil.createJWT(String.valueOf(user.getUser_id()), user.getUsername(), "user");
        HashMap map = new HashMap();
        map.put("token",token);
        map.put("username",user.getUsername());
        map.put("avatar",user.getAvatar());
        map.put("userId",user.getUser_id());
        map.put("nickName",user.getNick_name());
        map.put("expireTime",jwtUtil.parseJWT(token).getExpiration());
        return new Result(true,StatusCode.OK,"登陆成功",map);
    }

    /**
     * 发送短信
     * @param phone
     * @return
     */
    @GetMapping("/sendSms")
    public Result sendSms(@RequestParam String phone) {
        boolean result = loginService.sendSms(phone);
        if (result)
            return new Result(true, StatusCode.OK, "短信发送成功");
        return new Result(false, StatusCode.ERROR, "短信发送失败");
    }

    /**
     * 短信登录
     * @param phone
     * @param code
     * @return
     */
    @GetMapping("/loginBySms")
    public Result loginBySms(@RequestParam String phone, @RequestParam String code) {
        if (paramCheck(phone,code)!=null)
            return paramCheck(phone,code);
        User user = loginService.loginBySms(phone, code);
        if (user !=null ){
            String token = jwtUtil.createJWT(user.getUser_id().toString(), user.getNick_name(), "user");
            HashMap map = new HashMap();
            map.put("token",token);
            map.put("username",user.getUsername());
            map.put("avatar",user.getAvatar());
            return new Result(true, StatusCode.OK, "登录成功",map);
        }
        return new Result(false, StatusCode.ERROR, "登录失败");
    }

    /**
     * 短信注册
     * @param user
     * @param code 短信码
     * @return
     */
    @PostMapping("/registerBySms")
    public Result registerBySms(@RequestBody User user,@RequestParam String code) {
        if (paramCheck(user,code)!=null)
            return paramCheck(user,code);
        boolean result = loginService.registerBySms(user,code);
        if (result)
            return new Result(true, StatusCode.OK, "注册成功");
        return new Result(false, StatusCode.ERROR, "注册失败");
    }


    /**
     * 普通方式注册
     * @param user
     * @param code 图片验证码
     * @return
     */
    @PostMapping("/registerBySimple")
    public Result registerBySimple(@RequestBody User user ,@RequestParam String code){
         if (paramCheck(user,code)!=null)
             return paramCheck(user,code);
         boolean result = loginService.registerBySimple(user,code);
        if (result)
            return new Result(true, StatusCode.OK, "注册成功");
        return new Result(false, StatusCode.ERROR, "注册失败");

    }
    /**
     * 参数校验
     *
     * @param objects
     * @return
     */
    public Result paramCheck(Object... objects) {
        for (Object object : objects) {
            if (object == null) {
                return new Result(false, StatusCode.PARAM_ERROR, "参数异常");
            }
        }
        return null;
    }

    /**
     * 获取临时userId
     * @return
     */
    @GetMapping("/getTemporaryUserId")
    public Result getTemporaryUserId(){
        return new Result(loginService.getTemporaryUserId());
    }
}
